﻿using SCG.General;
using System;
using System.Net;

namespace SCG.SolarVengeanceEngine.Orders
{
    //An order that contains a MapObject as a target
    public class MapObjectOrder : StarShipOrder
    {
        //constructors        
        public MapObjectOrder(SVGame game, OrderType ot, SVMapObject svm)
            : base(game, ot)
        {
            MapObject = svm;
        }
        public MapObjectOrder(SVGame game, OrderType ot, SVTokenizer tok)
            : base(game, ot)
        {
            MapObject = tok.TokenMapObject;
        }

        //The MapObject being targeted
        public SVMapObject MapObject { get; set; }

        //Treat the map object as a StarSystem
        public StarSystem StarSystem
        {
            get
            {
                return MapObject as StarSystem;
            }
        }

        //Treat it as a StarShip
        public StarShip StarShip
        {
            get
            {
                return MapObject as StarShip;
            }
        }

        //target point
        public override SCGPoint TargetPoint
        {
            get
            {
                return MapObject.Location;
            }
        }

        //Persistence
        public override string Persist()
        {
            SVTokenizer tok = new SVTokenizer(Game);
            tok.AppendToken(MapObject);
            return base.Persist() + tok.Result;
        }

        //ToString
        public override string ToString()
        {
            switch(OrderType)
            {
                case OrderType.TargetStarSystem:
                case OrderType.TargetStarShip:
                case OrderType.TargetMapObject:
                    return "Target " + MapObject.DisplayName;
                case OrderType.Defend:
                    return "Defend " + MapObject.DisplayName;              
                case OrderType.Escort:
                    return "Escort " + MapObject.DisplayName;
                case OrderType.Unload:
                    return "Unload to " + MapObject.DisplayName;  
                case OrderType.BoardCarrier:
                    return "Board Carrier";
                default:
                    throw new ArgumentException("Unknown OrderType: " + OrderType);
            }
        }

        //does it target a starship?
        public override bool OrderTargetsStarShip
        {
            get
            {
                return MapObject is StarShip;
            }
        }
        public override StarShip StarShipTargeted
        {
            get
            {
                return MapObject as StarShip;
            }
        }
    }
}
